package com.ninegear.entity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;

import com.ninegear.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/26.
 */

public class Shell {
    private  float r;
    private PointF centerF;
    private float diameter;
    private List<PointF> fixPoints= new ArrayList<>();;
    private float sportAngular;
    private Bitmap mBitmap;
    public Shell(PointF centerF, float diameter, float sportAngular , Context context){
        this.centerF = centerF;
        this.diameter = diameter;
        r = diameter/6;
        mBitmap = ((BitmapDrawable) context.getDrawable(R.drawable.shell)).getBitmap();
        setSportAngular(sportAngular);
    }
    public void setSportAngular(float sportAngular) {
        this.sportAngular = sportAngular;
        setFixPoints();
    }
    /*
    5  6  7
    4  0  8
    3  2  1
     */
    private void setFixPoints() {
        fixPoints .clear();

        float d = (float) (diameter/Math.sqrt(2));
        fixPoints.add(centerF);
        for (int i =1 ;i<9 ; i++ ){
            float j = (i%2==0) ? d : diameter;
            PointF p = new PointF((float) (centerF.x+ ( j*Math.cos(sportAngular+ Math.PI*i/4))),(float) (centerF.y+j*Math.sin(sportAngular+ Math.PI*i/4)));
            fixPoints.add(p);
        }

    }

    public float getDiameter() {
        return diameter;
    }
    public float getR() {
        return r;
    }

    public PointF getCenterF() {
        return centerF;
    }

    public List<PointF> getFixPoints() {
        return fixPoints;
    }

    public void drawShell(Canvas canvas, Paint paintS) {
        Matrix mMatrix = new Matrix();
        int bwidth = mBitmap.getWidth();
        float tx = (float) (centerF.x-diameter/2*1.85);
        float ty = (float) (centerF.y-diameter/2*1.85);
        /*首先，将缩放为100*100。这里scale的参数是比例。有一点要注意，如果直接用100/
        bmp.getWidth()的话，会得到0，因为是整型相除，所以必须其中有一个是float型的，直接用100f就好。*/
        mMatrix.setScale((float) (1.85*diameter/mBitmap.getWidth()), (float) (1.85*diameter/mBitmap.getHeight()));
        //平移到（100，100）处
        mMatrix.postTranslate(tx, ty);
        mMatrix.postRotate((float) Math.toDegrees(sportAngular), centerF.x, centerF.y);
        canvas.drawBitmap(mBitmap,mMatrix,null);

    }
}
